﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AGVCallSystem
{
    public class CallDeviceBLL
    {
        public List<CallDeviceEntity> GetCallDevices()
        {
            CallDeviceDbContext callDB = new CallDeviceDbContext();
            return callDB.callDeviceEntities.ToList();
        }

        public List<CallDeviceEntity> GetCallByEnableState(bool enableCall)
        {
            CallDeviceDbContext callDB = new CallDeviceDbContext();
            return callDB.callDeviceEntities.Where(p => p.EnableCall == enableCall).ToList();
        }

        public List<CallDeviceEntity> GetCallByCallState(bool callState)
        {
            CallDeviceDbContext callDB = new CallDeviceDbContext();
            return callDB.callDeviceEntities.Where(p => p.CallState == callState).ToList();
        }

        public List<CallDeviceEntity> GetCallByExecuteState(bool executeState)
        {
            CallDeviceDbContext callDB = new CallDeviceDbContext();
            return callDB.callDeviceEntities.Where(p => p.ExecuteState == executeState).ToList();
        }

        public void AddCallDevice(CallDeviceEntity callDeviceEntity)
        {
            CallDeviceDbContext callDB = new CallDeviceDbContext();
            callDB.callDeviceEntities.Add(callDeviceEntity);
            callDB.SaveChanges();
        }

        public void DeleteCallDevice(CallDeviceEntity callDeviceEntity)
        {
            CallDeviceDbContext callDB = new CallDeviceDbContext();
            callDB.callDeviceEntities.Attach(callDeviceEntity);
            callDB.callDeviceEntities.Remove(callDeviceEntity);
            callDB.SaveChanges();
        }

        /// <summary>
        /// 编辑呼叫器设备
        /// </summary>
        /// <param name="callDeviceEntity"></param>
        public void EditCallDevice(CallDeviceEntity callDeviceEntity)
        {
            CallDeviceDbContext callDB = new CallDeviceDbContext();
            DbEntityEntry<CallDeviceEntity> entry = callDB.Entry<CallDeviceEntity>(callDeviceEntity);
            entry.State = System.Data.Entity.EntityState.Modified;
            callDB.SaveChanges();
        }

        /// <summary>
        /// 通过序号获取呼叫器按键
        /// </summary>
        /// <param name="callDeviceNum">呼叫器按键序号</param>
        /// <returns></returns>
        public CallDeviceEntity GetCallDeviceByNum(string callDeviceNum)
        {
            CallDeviceDbContext callDB = new CallDeviceDbContext();
            if (callDB.callDeviceEntities.Count(p => p.CallDeviceNum == callDeviceNum) > 0)
            {
                return callDB.callDeviceEntities.First(p => p.CallDeviceNum == callDeviceNum);
            }
            else
            {
                return null;
            }
        }
    }
}
